from wtforms import StringField,Form,IntegerField,FloatField
from wtforms.validators import length,ValidationError,input_required,number_range
from exts import db
from model.models import Dish,DishType

# 新增dish表单检验
class DishAddForm(Form):
    dish_name = StringField(validators=[length(min=1,max=20,message='请输入合适长度的菜名!')])
    price = FloatField(validators=[input_required(message='价格不能为空!'),number_range(min=0,max=100000,message='请输入正确的价格')])
    dish_type_no = IntegerField(validators=[input_required(message='菜品名不能为空!')])

    # 检验外键是否存在
    def validate_dish_type_no(self,field):
        dish_type=DishType.query.filter(DishType.dish_type_no==field.data).first()
        if dish_type:
            return True
        else:
            raise ValidationError("该菜品不存在!")


# 判断提交上来的信息
class UpdateDishForm(Form):
    dish_name = StringField(validators=[length(min=1,max=20,message="请输入合适长度的菜名"),input_required(message="菜名是必填项")])
    price = FloatField(validators=[input_required(message="价格不能为空"),number_range(min=0,max=100000,message='请输入正确的价格')])
    dish_type_no = IntegerField(validators=[input_required(message='菜品名不能为空')])

    # 检验外键是否存在
    def validate_dish_type_no(self,field):
        dish_type=DishType.query.filter(DishType.dish_type_no==field.data).first()
        if dish_type:
            return True
        else:
            raise ValidationError("该菜品不存在!")